import copy

# 定义一个名为Comment的类，包含三个属性：commentStr、startPos和endPos。
class Comment:
    commentStr = ''
    startPos = 0 # 记录注释的开始位置
    endPos = 0  # 记录注释的结束位置

def recognizer(code):
    """
    识别代码中的注释。
    
    Args:
        code (str): 待处理的代码字符串。
    
    Returns:
        list of Comment: 包含所有注释信息的列表。
    
    """
    outList = []
    state = ('1', '2', '3', '4', '5', '6', '7', '8', '0')  # 有9个状态
    state = 1  # 初始状态为1
    comment = Comment()
    comment.commentStr = ''
    
    for i in range(len(code)):
        char = code[i]
        match state:
            case 1:
                if char == '/':
                    state = 2  # 状态2：待定
                    comment.commentStr = comment.commentStr + char
                    comment.startPos = i
                pass
            case 2:
                comment.commentStr = comment.commentStr + char
                if  char == '/':
                    state = 3  # 状态3：单行注释开始    
                elif  char == '*':
                    state = 6  # 状态6：多行注释开始   
                else:
                    state = 1
                    comment.commentStr = ''
                pass
            case 3:
                comment.commentStr = comment.commentStr + char
                if char != '\n':
                    state = 4 #状态4：单行注释
                elif char == '\n':
                    state = 5#状态5：单行注释结束
                    comment.endPos = i
                pass
            case 4:   
                comment.commentStr = comment.commentStr + char
                if char != '\n':
                    state = 4#状态4：单行注释
                elif char == '\n':
                    state = 5#状态5：单行注释结束
                    comment.endPos = i
                pass
            case 5:          
                outList.append(copy.deepcopy(comment))
                comment.commentStr = ''
                state = 1
                pass
            case 6:
                comment.commentStr = comment.commentStr + char
                if  char != '*':
                    state = 7  #状态7：多行注释   
                elif  char == '*':
                    state = 8  #状态8：待定
                pass
            case 7:
                comment.commentStr = comment.commentStr + char
                if  char != '*':
                    state = 7  #状态7：多行注释   
                elif  char == '*':
                    state = 8  #状态8：待定
                pass
            case 8:    
                comment.commentStr = comment.commentStr + char
                if  char == '/':
                    state = 9  #多行注释结束
                    comment.endPos = i
                elif  char == '*':
                    state = 7  #状态7：多行注释
                    pass
            case 9:
                outList.append(copy.deepcopy(comment))
                comment.commentStr = ''
                state = 1 #状态9：多行注释结束
                pass
    return outList

if __name__ == '__main__':

    # 使用示例
    #col 0-33: 多行注释结束,col 57-79: 单行注释
    code = "/* This is a multi-line comment */int main() { // This is a single line comment\nreturn 0; }"
    print(code)

    cmt = recognizer(code)
    for c in cmt:
        print(c.commentStr)
        print(c.startPos, c.endPos)
    # print(cmt)

#删除注释
def remove_comments(code, commentsList):
    """
    从代码字符串中移除注释。
    
    Args:
        code (str): 包含注释的代码字符串。
        commentsList (list of Comment): 包含所有注释信息的列表，每个注释对象都有 startPos 和 endPos 属性，
                                       分别表示注释在代码字符串中的起始和结束位置。
    
    Returns:
        str: 移除了注释后的代码字符串。
    
    """
    cmt = copy.deepcopy(commentsList)
    cmt.reverse()
    new_s = code
    for c in cmt:
        new_s = new_s[:c.startPos] + new_s[c.endPos+1:]
    return new_s

if __name__ == '__main__':
    #删除注释后的代码
    new_s = remove_comments(code, cmt)
    print(new_s)